package com.gpoint.rdp.door.logs.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gpoint.rdp.base.core.util.string.StringUtil;
import com.gpoint.rdp.base.core.entity.ResultMessage;
import com.gpoint.rdp.base.framework.page.PageList;
import com.gpoint.rdp.base.framework.page.PageResult;
import com.gpoint.rdp.api.base.query.QueryFilter;
import com.gpoint.rdp.base.web.controller.GenericController;
import com.gpoint.rdp.base.web.json.PageJson;
import com.gpoint.rdp.base.web.util.RequestUtil;
import com.gpoint.rdp.door.logs.service.SwipeRecordService;
import com.gpoint.rdp.door.user.persistence.dao.CampusCardQueryDao;

import cn.pluss.platform.shiro.DefaultPartyUserPo;
import cn.pluss.platform.shiro.SSORealm;

import com.gpoint.rdp.door.logs.service.SwipeRecordQueryService;
import com.gpoint.rdp.door.logs.persistence.dao.SwipeRecordQueryDao;
import com.gpoint.rdp.door.logs.persistence.entity.SwipeRecordPo;
import com.gpoint.rdp.base.core.util.JacksonUtil;

import java.util.List;

/**
 * TB_DOOR_SWIPERECORD 控制类
 *
 *<pre> 
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-09-30 18:57:54
 *</pre>
 */
@Controller
@RequestMapping("/rdp/logs/swipeRecord/")
public class SwipeRecordController extends GenericController{
	@Resource
	private SwipeRecordService swipeRecordService;
	@Resource
	private SwipeRecordQueryService swipeRecordQueryService;
	
	
	
	

	
	
	@RequestMapping("list")
	 public ModelAndView list(HttpServletRequest request,HttpServletResponse response) throws Exception{
	  ModelAndView mv=getAutoView();
	  DefaultPartyUserPo   partyUserPo=SSORealm.queryCurrentShiroUser();
		System.out.println(partyUserPo.getAccount());
		System.out.println(partyUserPo.getPassword());
		System.out.println(partyUserPo.getAddress());
		
	  return mv;
	  
	 }
	
	
	/**
	 * 【TB_DOOR_SWIPERECORD】列表(分页条件查询)数据
	 *
	 * @param request
	 * @param reponse
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("listJson")
	public @ResponseBody PageJson listJson(HttpServletRequest request,HttpServletResponse reponse) throws Exception{
		QueryFilter queryFilter = getQuerFilter(request);
		try{
			String begindate = RequestUtil.getString(request,"Q^READDATE^DL");
			 String enddate=RequestUtil.getString(request, "Q^READDATE^DG");
			//拼凑SQL：
			 if(begindate.length()!=0 && enddate.length()!=0 ) {
				 String sql =   "READDATE >=  '"+begindate+"'"+"and READDATE <=  '"+enddate+"'";
				 queryFilter.addParamsFilter("begin_READDATE", begindate);
				 queryFilter.addParamsFilter("end_READDATE", enddate);
				 queryFilter.addParamsFilter("whereSql", sql);
			 }
			
			 //
			 if(begindate.length()!=0 && enddate.length()==0 ) {
				 String sql =   "READDATE >=  '"+begindate+"'";
				 queryFilter.addParamsFilter("begin_READDATE", begindate);
				 queryFilter.addParamsFilter("whereSql", sql);
			 }
			 
			 //
			 if(begindate.length()==0 && enddate.length()!=0 ) {
				 String sql =   "READDATE <=  '"+enddate+"'";
				 queryFilter.addParamsFilter("end_READDATE", enddate);
				 queryFilter.addParamsFilter("whereSql", sql);
			 }
			 
			 //都是空的 直接查出全部
			 //调用service 方法
			 String listData  =    this.swipeRecordQueryService.queryPage(queryFilter);
			 
			 
			 PageList<SwipeRecordPo> swipeRecordList = null;
			if(JacksonUtil.isJsonObject(listData)){
				List<SwipeRecordPo> list = SwipeRecordPo.fromJsonArrayString(JacksonUtil.getString(listData, "data"));
				PageResult pageResult = PageResult.fromJson(JacksonUtil.getString(listData, "pageResult"));
				swipeRecordList = new PageList<SwipeRecordPo>(list, pageResult);
				
			}
			
			return new PageJson(swipeRecordList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new PageJson();
		}
	}
	
	/**
	 * 编辑【TB_DOOR_SWIPERECORD】信息页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String recid=RequestUtil.getString(request, "recid");
		SwipeRecordPo swipeRecord=null;
		if(StringUtil.isNotEmpty(recid)){
			String data = null;
			data = swipeRecordQueryService.get(recid);
			swipeRecord = SwipeRecordPo.fromJsonString(data);
		}

		return getAutoView().addObject("swipeRecord", swipeRecord).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 编辑【TB_DOOR_SWIPERECORD】信息页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("flowEdit")
	public ModelAndView flowEdit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String recid=RequestUtil.getString(request, "recid");
		SwipeRecordPo swipeRecord=null;
		if(StringUtil.isNotEmpty(recid)){
			String data = null;
			data = swipeRecordQueryService.get(recid);
			swipeRecord = SwipeRecordPo.fromJsonString(data);
		}
		return getAutoView().addObject("swipeRecord", swipeRecord).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 【TB_DOOR_SWIPERECORD】明细页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("get")
	public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String recid=RequestUtil.getString(request, "recid");
		SwipeRecordPo swipeRecord=null;
		if(StringUtil.isNotEmpty(recid)){
			String data = null;
			data = swipeRecordQueryService.get(recid);
			swipeRecord = SwipeRecordPo.fromJsonString(data);
		}
		return getAutoView().addObject("swipeRecord", swipeRecord).addObject("returnUrl", preUrl);
	}
	
	/** 
	 * 保存【TB_DOOR_SWIPERECORD】信息
	 *
	 * @param request
	 * @param response
	 * @param  swipeRecord
	 * @throws Exception
	 */
	@RequestMapping("save")
	public void save(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			String json = RequestUtil.getString(request, "json");
			swipeRecordService.save(json);
			message=new ResultMessage(ResultMessage.SUCCESS, "保存TB_DOOR_SWIPERECORD成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "对TB_DOOR_SWIPERECORD操作失败,"+e.getMessage());
			logger.error("对TB_DOOR_SWIPERECORD操作失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	
	/**
	 *  批量删除【TB_DOOR_SWIPERECORD】记录
	 *
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("remove")
	public void remove(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			//获得待删除的id
			String[] ids=RequestUtil.getStringAryByStr(request, "recid");
			swipeRecordService.deleteByIds(ids);
			message=new ResultMessage(ResultMessage.SUCCESS, "删除TB_DOOR_SWIPERECORD成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "删除TB_DOOR_SWIPERECORD失败，" + e.getMessage());
			logger.error("删除TB_DOOR_SWIPERECORD失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	

}